from django.shortcuts import render

from rest_framework import viewsets
from rest_framework import filters
from django_filters.rest_framework import DjangoFilterBackend
# from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated

from .models import Testcase, Apicase
from .filters import TestcaseFilter
from .serializers import CaseSerializer, ApicaseSerializer
from .permission import IsOwnerOrReadOnly


class TestcaseListViewset(viewsets.ModelViewSet):
    """
    用例列表页
    """
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    authentication_classes = (SessionAuthentication,)
    queryset = Testcase.objects.all()
    serializer_class = CaseSerializer
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = TestcaseFilter
    search_fields = ('title', )
    ordering_fields = ('id',)


class ApicaseViewset(viewsets.ModelViewSet):
    """
    接口测试
    """
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    authentication_classes = (SessionAuthentication,)
    queryset = Apicase.objects.all()
    serializer_class = ApicaseSerializer